package com.lw.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.commonutil.vo.CourseVo;
import com.lw.commonutil.vo.UcenterMemberVo;
import com.lw.commonutil.result.ResultCodeEnum;
import com.lw.order.client.CourseFeignClient;
import com.lw.order.client.UcenterFeignClient;
import com.lw.order.entity.Order;
import com.lw.order.mapper.OrderMapper;
import com.lw.order.service.OrderService;
import com.lw.order.utils.OrderNoUtil;
import com.lw.servicebase.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private CourseFeignClient courseFeignClient;
    @Autowired
    private UcenterFeignClient ucenterFeignClient;
    @Override
    public String createOrder(String courseId, String memberId) {
        //判断订单是否存在 若存在直接返回当前订单号
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq( "course_id",courseId);
        wrapper.eq( "member_id",memberId );
        Order orderExist = baseMapper.selectOne( wrapper );
        if (orderExist!=null){
            return orderExist.getOrderNo();
        }
        //远程调用获取课程信息
        CourseVo course = courseFeignClient.getCourse(courseId);
        //远程调用获取用户信息
        UcenterMemberVo member = ucenterFeignClient.profile( memberId );
        //创建对单对象
        Order order = new Order();
        order.setCourseCover( course.getCover() );
        order.setCourseId( courseId );
        order.setCourseTitle( course.getTitle() );
        order.setMemberId( memberId );
        order.setMobile( member.getMobile() );
        order.setNickname( member.getNickname() );
        order.setTotalFee( course.getPrice() );
        order.setPayType( 1 );
        order.setStatus( 0 );//未支付
        //生成订单号
        order.setOrderNo( OrderNoUtil.getOrderNo() );
        baseMapper.insert( order );
        return order.getOrderNo();
    }
}
